//
//  BKMain_thwartwiseGoodsCell.m
//  bestkeep
//
//  Created by utouu666 on 17/2/22.
//  Copyright © 2017年 utouu. All rights reserved.
//

#import "BKMain_thwartwiseGoodsCell.h"
#import "BKMain_GoodsViewCell.h"
#import "BKMainCommon.h"
@interface BKMain_thwartwiseGoodsCell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSArray *resultArray;

@end

@implementation BKMain_thwartwiseGoodsCell

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initViews];
    }
    return self;
}

-(void)initViews{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    
    [_collectionView registerClass:[BKMain_GoodsViewCell class] forCellWithReuseIdentifier:@"BKMain_GoodsViewCell"];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.scrollIndicatorInsets = UIEdgeInsetsMake(20, 20,20, 20);
    _collectionView.showsHorizontalScrollIndicator = NO;
    [self addSubview:_collectionView];
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left);
        make.right.mas_equalTo(self.mas_right);
        make.top.mas_equalTo(self.mas_top);
        make.bottom.mas_equalTo(self.mas_bottom);
    }];
}
- (void)fillData:(id)data{
    if (!data) {
        return;
    }
    self.resultArray = data;
    [self.collectionView reloadData];
}

+(CGSize)calculateSize:(id)data{
    if(!data || ![data isKindOfClass:[NSArray class]]) {
        return CGSizeMake(0, 0);
    }
    return CGSizeMake(SCREENWIDTH, 145*SCREENWIDTH/375);
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return _resultArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    BKMain_GoodsViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BKMain_GoodsViewCell" forIndexPath:indexPath];
    NSDictionary *dic = self.resultArray[indexPath.section];
    NewModel *model = [[NewModel alloc]init];
    [model setValuesForKeysWithDictionary:dic];
    [cell.goodsImageView load:model.goodsCoverImg placeholderImage:[UIImage imageNamed:@"BKMain_newGoods"]];
    cell.goodsDetail.text = model.goodsName;
    if (model.upText == nil) {
        model.upText = @"";
    }
    
    cell.goodsPrice.attributedText = [BKMainCommon specialStringStyle:[NSString stringWithFormat:@"%@%@", model.goodsPrice, model.upText] fontOfSize:10];
    

    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    
    return CGSizeMake(0.1, 157*SCREENWIDTH/375.f);
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (SCREENWIDTH< 375) {
        return CGSizeMake((SCREENWIDTH -12)/ 3.f - 10 - 15, 157*SCREENWIDTH/375.f + 5);
    }else{
        return CGSizeMake((SCREENWIDTH -12)/ 3.f - 10 - 15, 157*SCREENWIDTH/375.f);
    }
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 0 ) {
        return UIEdgeInsetsMake(0, 12, 0, 5);
    }else if (section == self.resultArray.count -1){
        return UIEdgeInsetsMake(0, 5, 0, 12);
    }else{
        return UIEdgeInsetsMake(0, 5, 0, 5);
    }
    
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    BKMainViewController *mainPage  = (BKMainViewController *)self.nextResponder.nextResponder.nextResponder;
    NSDictionary *dic = self.resultArray[indexPath.section];
    NewModel *model = [[NewModel alloc]init];
    [model setValuesForKeysWithDictionary:dic];
    Mediator *mediator = [Mediator sharedInstance];
    UIViewController *detailVC = [mediator BKGD_goodsDetailViewController:model.goodsId saleTypes:model.saleTypes];
    detailVC.view.backgroundColor = [UIColor whiteColor];
    [mainPage.navigationController pushViewController:detailVC animated:YES];
}

@end
